//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.UI.WebControls
{
	public class ImageCommandColumnTemplate : ITemplate
	{
		private string mCommandName;
		private bool mDesignMode;
		private ImageCommandColumnEditMode mEditMode = ImageCommandColumnEditMode.Command;
		private string mImageURL;
		private ListItemType mItemType = ListItemType.Item;
		private string mKeyField;
		private string mNavigateURL;
		private string mNavigateURLFormatString;
		private string mOnClickJS;
		private bool mShowImage = true;
		private string mText;
		private bool mVisible = true;
		private string mVisibleField;
		public ImageCommandColumnTemplate() : this(ListItemType.Item)
		{
		}
		public ImageCommandColumnTemplate(ListItemType itemType)
		{
			this.ItemType = itemType;
		}
		public string CommandName {
			get { return mCommandName; }
			set { mCommandName= value; }
		}
		public bool DesignMode {
			get { return mDesignMode; }
			set { mDesignMode= value; }
		}
		public ImageCommandColumnEditMode EditMode {
			get { return mEditMode; }
			set { mEditMode= value; }
		}
		public string ImageURL {
			get { return mImageURL; }
			set { mImageURL= value; }
		}
		public ListItemType ItemType {
			get { return mItemType; }
			set { mItemType= value; }
		}
		public string KeyField {
			get { return mKeyField; }
			set { mKeyField= value; }
		}
		public string NavigateURL {
			get { return mNavigateURL; }
			set { mNavigateURL= value; }
		}
		public string NavigateURLFormatString {
			get { return mNavigateURLFormatString; }
			set { mNavigateURLFormatString= value; }
		}
		public string OnClickJS {
			get { return mOnClickJS; }
			set { mOnClickJS= value; }
		}
		public bool ShowImage {
			get { return mShowImage; }
			set { mShowImage= value; }
		}
		public string Text {
			get { return mText; }
			set { mText= value; }
		}
		public bool Visible {
			get { return mVisible; }
			set { mVisible= value; }
		}
		public string VisibleField {
			get { return mVisibleField; }
			set { mVisibleField= value; }
		}
		private bool GetIsVisible(DataGridItem container)
		{
			bool isVisible;
			if (!String.IsNullOrEmpty(VisibleField)) {
				isVisible = Convert.ToBoolean(DataBinder.Eval(container.DataItem, VisibleField));
			} else {
				isVisible = Visible;
			}
			return isVisible;
		}
		private int GetValue(DataGridItem container)
		{
			int keyValue = Null.NullInteger;
			if (!String.IsNullOrEmpty(KeyField)) {
				keyValue = Convert.ToInt32(DataBinder.Eval(container.DataItem, KeyField));
			}
			return keyValue;
		}
		private void Item_DataBinding(object sender, System.EventArgs e)
		{
			DataGridItem container;
			int keyValue = Null.NullInteger;
			if (EditMode == ImageCommandColumnEditMode.URL) {
				HyperLink hypLink = (HyperLink)sender;
				container = (DataGridItem)hypLink.NamingContainer;
				keyValue = GetValue(container);
				if (!String.IsNullOrEmpty(NavigateURLFormatString)) {
					hypLink.NavigateUrl = string.Format(NavigateURLFormatString, keyValue);
				} else {
					hypLink.NavigateUrl = keyValue.ToString();
				}
			} else {
				if (!String.IsNullOrEmpty(ImageURL) && ShowImage) {
					ImageButton colIcon = (ImageButton)sender;
					container = (DataGridItem)colIcon.NamingContainer;
					keyValue = GetValue(container);
					colIcon.CommandArgument = keyValue.ToString();
					colIcon.Visible = GetIsVisible(container);
				}
				if (!String.IsNullOrEmpty(Text) && !ShowImage) {
					LinkButton colLink = (LinkButton)sender;
					container = (DataGridItem)colLink.NamingContainer;
					keyValue = GetValue(container);
					colLink.CommandArgument = keyValue.ToString();
					colLink.Visible = GetIsVisible(container);
				}
			}
		}
		public void InstantiateIn(Control container)
		{
			switch (ItemType) {
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:
				case ListItemType.EditItem:
					if (EditMode == ImageCommandColumnEditMode.URL) {
						HyperLink hypLink = new HyperLink();
						hypLink.ToolTip = Text;
						if (!String.IsNullOrEmpty(ImageURL) && ShowImage) {
							System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
							if (DesignMode) {
								img.ImageUrl = ImageURL.Replace("~/", "../../");
							} else {
								img.ImageUrl = ImageURL;
							}
							hypLink.Controls.Add(img);
							img.ToolTip = Text;
						} else {
							hypLink.Text = Text;
						}
						hypLink.DataBinding += Item_DataBinding;
						container.Controls.Add(hypLink);
					} else {
						if (!String.IsNullOrEmpty(ImageURL) && ShowImage) {
							ImageButton colIcon = new ImageButton();
							if (DesignMode) {
								colIcon.ImageUrl = ImageURL.Replace("~/", "../../");
							} else {
								colIcon.ImageUrl = ImageURL;
							}
							colIcon.ToolTip = Text;
							if (!String.IsNullOrEmpty(OnClickJS)) {
								ClientAPI.AddButtonConfirm(colIcon, OnClickJS);
							}
							colIcon.CommandName = CommandName;
							colIcon.DataBinding += Item_DataBinding;
							container.Controls.Add(colIcon);
						}
						if (!String.IsNullOrEmpty(Text) && !ShowImage) {
							LinkButton colLink = new LinkButton();
							colLink.ToolTip = Text;
							if (!String.IsNullOrEmpty(OnClickJS)) {
								ClientAPI.AddButtonConfirm(colLink, OnClickJS);
							}
							colLink.CommandName = CommandName;
							colLink.Text = Text;
							colLink.DataBinding += Item_DataBinding;
							container.Controls.Add(colLink);
						}
					}
					break;
				case ListItemType.Footer:
				case ListItemType.Header:
					container.Controls.Add(new LiteralControl(Text));
					break;
			}
		}
	}
}
